| Total Complexity | 3 | 
| Total Lines | 25 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
| 1 | import {Inject} from '@nestjs/common'; | ||
| 8 | |||
| 9 | @QueryHandler(DownloadFileQuery) | ||
| 10 | export class DownloadFileQueryHandler { | ||
| 11 | constructor( | ||
| 12 |     @Inject('IFileRepository') | ||
| 13 | private readonly fileRepository: IFileRepository, | ||
| 14 |     @Inject('IFileStorage') | ||
| 15 | public readonly fileStorage: IFileStorage | ||
| 16 |   ) {} | ||
| 17 | |||
| 18 |   public async execute(query: DownloadFileQuery): Promise<DownloadedFileView> { | ||
| 19 | const file = await this.fileRepository.findOneById(query.id); | ||
| 20 |     if (!file) { | ||
| 21 | throw new FileNotFoundException(); | ||
| 22 | } | ||
| 23 | |||
| 24 | const buffer = await this.fileStorage.download(file); | ||
| 25 |     if (!buffer) { | ||
| 26 | throw new FileNotFoundException(); | ||
| 27 | } | ||
| 28 | |||
| 29 | return new DownloadedFileView( | ||
| 30 | file.getOriginalName(), | ||
| 31 | file.getMimeType(), | ||
| 32 | buffer | ||
| 33 | ); | ||
| 36 |